package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.administrators.Log;
import com.cskaoyan.bean.administrators.bo.AdminCreateBO;
import com.cskaoyan.bean.administrators.bo.RoleCreateBO;
import com.cskaoyan.bean.administrators.bo.RolePermissionsBO;
import com.cskaoyan.bean.administrators.bo.RoleUpdateBO;
import com.cskaoyan.bean.administrators.vo.*;
import com.cskaoyan.bean.promotion.storage.UploadPicStorageVO;
import com.cskaoyan.service.AdministratorsService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description: 系统管理
 * @Author: 徐能雄
 * @Date: 2021/9/15 15:11
 */
@RestController
@RequestMapping("admin")
public class AdministratorsController {

    @Autowired
    AdministratorsService administratorsService;

    //管理员
    @GetMapping("/admin/list")
    BaseRespVo adminList(BaseParam param, String username) {
        BaseData<AdminListVO> data = administratorsService.adminList(param, username);
        return BaseRespVo.ok(data);
    }

    @PostMapping("/admin/create")
    BaseRespVo adminCreate(@RequestBody AdminCreateBO adminCreateBO) {
        AdminCreateVO adminCreateVO = administratorsService.adminCreate(adminCreateBO);
        return BaseRespVo.ok(adminCreateVO);
    }

    @PostMapping("/admin/delete")
    BaseRespVo adminDelete(@RequestBody AdminCreateBO deleteCreateBO) {
        int code = administratorsService.adminDelete(deleteCreateBO);
        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("删除失败");
        }
    }

    @PostMapping("/admin/update")
    BaseRespVo adminUpdate(@RequestBody AdminCreateBO updateCreateBO) {
        AdminUpdateVO adminUpdateVO = administratorsService.adminUpdate(updateCreateBO);
        return BaseRespVo.ok(adminUpdateVO);
    }


    //日志
    @GetMapping("/log/list")
    BaseRespVo logList(BaseParam param, String username) {
        BaseData<Log> data = administratorsService.logList(param, username);
        return BaseRespVo.ok(data);
    }


    //角色
    @GetMapping("/role/options")
    BaseRespVo roleOptions() {
        RoleOptionsVO roleOptionsVO = administratorsService.roleOptions();
        return BaseRespVo.ok(roleOptionsVO);
    }

    @GetMapping("/role/list")
    BaseRespVo roleList(BaseParam param, String name) {
        BaseData<RoleListVO> data = administratorsService.roleList(param, name);
        return BaseRespVo.ok(data);
    }

    @PostMapping("/role/create")
    BaseRespVo roleCreate(@RequestBody RoleCreateBO roleCreateBO) {
        RoleCreateVO date = administratorsService.roleCreate(roleCreateBO);
        return BaseRespVo.ok(date);
    }

    @PostMapping("/role/delete")
    BaseRespVo roleDelete(@RequestBody RoleUpdateBO roleDeleteBO) {
        int code = administratorsService.roleDelete(roleDeleteBO);
        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("删除失败");
        }
    }

    @PostMapping("/role/update")
    BaseRespVo roleUpdate(@RequestBody RoleUpdateBO roleUpdateBO) {
        int code = administratorsService.roleUpdate(roleUpdateBO);
        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("修改失败");
        }
    }

    @GetMapping("/role/permissions")
    BaseRespVo roleGetPermissions(Integer roleId) throws JsonProcessingException {
        RolePermissionsVO rolePermissionsVO = administratorsService.roleGetPermissions(roleId);
        return BaseRespVo.ok(rolePermissionsVO);
    }

    @PostMapping("/role/permissions")
    BaseRespVo rolePostPermissions(@RequestBody RolePermissionsBO rolePermissionsBO) {
        int code = administratorsService.rolePostPermissions(rolePermissionsBO);
        if (code == 200){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail("授权出错，请重新操作");
        }
    }

    //对象
    @GetMapping("/storage/list")
    BaseRespVo storageList(BaseParam param, String key, String name) {
        BaseData<StorageListVO> data = administratorsService.storageList(param, key, name);
        return BaseRespVo.ok(data);
    }

//    @PostMapping("/storage/create")
//    BaseRespVo storageCreate(@RequestParam("file") MultipartFile myFile) throws IOException {
//        Storage storage = administratorsService.storageCreate(myFile);
//        if(storage == null) return BaseRespVo.fail();
//        return BaseRespVo.ok(storage);
//    }

    @PostMapping("/storage/delete")
    BaseRespVo storageDelete(@RequestBody StorageListVO storageDeleteBO) {
        int code = administratorsService.storageDelete(storageDeleteBO);
        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("修改失败");
        }
    }

    @PostMapping("/storage/update")
    BaseRespVo storageUpdate(@RequestBody StorageListVO storageUpdateBO) {
        StorageListVO storage = administratorsService.storageUpdate(storageUpdateBO);
        return BaseRespVo.ok(storage);
    }


    @RequestMapping("/storage/create")
    public BaseRespVo uploadFile(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return BaseRespVo.fail("上传文件为空");
        }

        String type = file.getContentType();
        //判断后缀
        if (!("image/png".equals(type) || "image/jpg".equals(type) || "image/jpeg".equals(type))) {
            return BaseRespVo.fail("必须上传png或jpg文件！");
        }
        //判断大小
        if (file.getSize() > 1024 * 1024) {
            return BaseRespVo.fail("文件大小超出限制！");
        }

        UploadPicStorageVO storageVO = administratorsService.setPic(file);

        return BaseRespVo.ok(storageVO);

    }


}
